package com.yidiandao.business.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.ShareEntity;
import com.yidiandao.business.utils.ShareSDKUtils;
import com.yidiandao.business.utils.UIUtils;
import com.yidiandao.business.utils.UmengUtils;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * 分享的dialog
 * <p>
 * Created by SunDing(dindding@163.com) on 2017/3/15 9:50
 */
public class ShareDialog extends Dialog implements View.OnClickListener {

    private Context mContext;

    private PlatformActionHelper mListener;
    /**
     * 是否是分享图片
     */
    private boolean mIsSharePic = false;

    private String mPath;

    private ShareEntity mShareEntity;

    public ShareDialog(Context context) {
        super(context, R.style.ThemeNoTitleDialog);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pop_share_sdk);
        initWindowStyle();
        initViews();
    }

    private void initViews() {
        LinearLayout weChat = (LinearLayout) findViewById(R.id.ll_wechat);
        LinearLayout weChatMoment = (LinearLayout) findViewById(R.id.ll_wechat_moment);
        LinearLayout qq = (LinearLayout) findViewById(R.id.ll_qq);
        LinearLayout QZone = (LinearLayout) findViewById(R.id.ll_qzone);

        weChat.setOnClickListener(this);
        weChatMoment.setOnClickListener(this);
        qq.setOnClickListener(this);
        QZone.setOnClickListener(this);
    }

    private void initWindowStyle() {
        // 改变样式-->window的样式
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            // 设置弹出位置
            params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;

            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;

            window.setAttributes(params);
        }
    }

    /**
     * 分享图片
     *
     * @param path 图片路径
     */
    public void setSharePicPath(String path) {
        if (TextUtils.isEmpty(path)) {
            dismiss();
            return;
        }

        mPath = path;
        mIsSharePic = true;
    }

    /**
     * 分享图片  带分享的回调
     *
     * @param path     图片路径
     * @param listener 分享结果的监听
     */
    public void setSharePicWithCallBack(String path, PlatformActionHelper listener) {
        setSharePicPath(path);
        mListener = listener;
    }

    /**
     * 分享链接
     *
     * @param shareEntity 分享的bean
     */
    public void setShareLinkData(ShareEntity shareEntity) {
        if (null == shareEntity || TextUtils.isEmpty(shareEntity.getUrl())) {
            dismiss();
            return;
        }
        mIsSharePic = false;
        mShareEntity = shareEntity;
    }

    /**
     * 分享链接  带分享的回调
     *
     * @param shareEntity 分享的bean
     * @param listener  分享结果的监听
     */
    public void setShareLinkDataWithCallBack(ShareEntity shareEntity, PlatformActionHelper listener) {
        setShareLinkData(shareEntity);
        mListener = listener;
    }

    @Override
    public void onClick(View v) {
        String platName = null;
        switch (v.getId()) {
            case R.id.ll_wechat:
                //微信好友
                platName = Wechat.NAME;
                UmengUtils.onEvent(mContext, R.string.umeng_share_Wechat);
                break;
            case R.id.ll_wechat_moment:
                //微信朋友圈
                UmengUtils.onEvent(mContext, R.string.umeng_share_WechatMoments);
                platName = WechatMoments.NAME;
                break;
            case R.id.ll_qq:
                //qq
                UmengUtils.onEvent(mContext, R.string.umeng_share_QQ);
                platName = QQ.NAME;
                break;
            case R.id.ll_qzone:
                //qq空间
                UmengUtils.onEvent(mContext, R.string.umeng_share_qzone);
                platName = QZone.NAME;
                break;
                /*  case R.id.ll_weibo:
                //微博
                 UmengEventUtils.onEvent(mContext,R.string.umeng_share_weibo);
                platName =SinaWeibo.NAME;
                break;*/
        }

        //判断是分享图片还是链接
        if (mIsSharePic) {
            ShareSDKUtils.sharePic(UIUtils.getContext(), platName, mPath, mListener);
        } else {
            ShareSDKUtils.shareLinked(UIUtils.getContext(), platName, mShareEntity, mListener);
        }

        dismiss();
    }

    public static class PlatformActionHelper implements PlatformActionListener {

        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
        }

        @Override
        public void onCancel(Platform platform, int i) {
        }
    }
}
